package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;

@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues 声明需要监听的所有队列
     * org.springframework.amqp.core.Message
     * 参数可以写以下类型
     * 1.Message message:原生消息详细信息。头+体
     * 2.T content:发送的消息的类型OrderReturnReasonEntity content
     * 3.Channel channel:当前传输数据的通道
     *
     * Queue:可以有很多接受者，最终只有一个能收到消息，只要收到消息，队列删除消息
     * 场景：
     *  1）订单服务启动多个:同一个消息只能有一个客户端收到
     *  2）只有一个消息完全处理完，方法运行结束 才可以接收到下一个消息
     */
    @RabbitHandler
    public void reveive(Message message, OrderReturnReasonEntity content,
                        Channel channel){
        System.out.println("接收的消息内容..."+message+"==>内容："+content);

//        try {
//            TimeUnit.SECONDS.sleep(3);
//        } catch(InterruptedException e) {
//            e.printStackTrace();
//        }

        System.out.println("消息处理完成==>"+content.getName());
        //channel内按顺序自增的
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag["+deliveryTag+"]");
        //签收消息,非批量模式
        try {
            if(deliveryTag%2==0){
                //收货
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了["+deliveryTag+"]号货物");
            }else {
                //退货 requeue=false 丢弃 requeue=true 发回服务器，服务器重新入队
                channel.basicNack(deliveryTag,false,true);
//                channel.basicReject();
                System.out.println("没有签收["+deliveryTag+"]号货物");
            }

        } catch (Exception e){
            //网络中断了
        }

    }


    @RabbitHandler
    public void reveive2(OrderEntity content){
        System.out.println("接收的消息内容...内容："+content);

    }
}